#!/bin/bash

logs=(
debug.log
smsboxdebug.log
smsbox.log
traffic.log
wapbox.log
)

# stop kannel first
$HOME/src/kannel-ss.sh stop > /dev/null

# rotate logs
yesterday=$(date --date '1 day ago' +%y%m%d)

for log in ${logs[*]}
do
    s_dir="$HOME/log"
    t_dir="$HOME/log/${yesterday:0:4}/$yesterday" 

    s_log="$s_dir/$log"
    t_log="$t_dir/$yesterday.$log"

    if [[ -e $s_log ]]
    then
        if [[ ! -d $t_dir ]]
        then
            mkdir -p $t_dir
            chown kannel.ht $t_dir
            chmod 755 $t_dir
        fi
        
        mv $s_log $t_log
    fi
done

# start kannel
$HOME/src/kannel-ss.sh start > /dev/null

# change owner/group of newly created logs
sleep 30
chown kannel.ht $HOME/log/*
